package com.gzy.literature.controller;

import com.github.pagehelper.PageInfo;
import com.gzy.literature.entity.UserRecommendations;
import com.gzy.literature.service.UserRecommendationsService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;

@RestController
@RequestMapping("userRecommendations")
public class UserRecommendationsController extends HttpServlet {

    @Resource
    private UserRecommendationsService userRecommendationsService;

    /**
     * 分页查询
     *
     * @param userRecommendations 筛选条件
     * @param pageNum            页码（从1开始）
     * @param pageSize           每页显示的记录数
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<PageInfo<UserRecommendations>> queryByPage(UserRecommendations userRecommendations,
                                                                     @RequestParam(defaultValue = "1") int pageNum,
                                                                     @RequestParam(defaultValue = "10") int pageSize) {
        try {
            PageInfo<UserRecommendations> pageInfo = this.userRecommendationsService.queryCll(userRecommendations, pageNum, pageSize);
            return ResponseEntity.ok(pageInfo);
        } catch (Exception e) {
            // 记录日志或处理异常
            return ResponseEntity.status(500).body(null); // 或者返回一个错误消息对象
        }
    }

    /**
     * 通过主键查询单条数据
     *
     * @param userId 主键
     * @return 单条数据
     */
    @GetMapping("{userId}")
    public ResponseEntity<UserRecommendations> queryById(@PathVariable("userId") Integer userId) {
        try {
            UserRecommendations userRecommendations = this.userRecommendationsService.queryById(userId);
            return ResponseEntity.ok(userRecommendations);
        } catch (Exception e) {
            // 记录日志或处理异常
            return ResponseEntity.status(404).body(null); // 如果没有找到，可以返回404
        }
    }

    /**
     * 新增数据
     *
     * @param userRecommendations 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<UserRecommendations> add(@RequestBody UserRecommendations userRecommendations) {
        try {
            // 可以添加验证逻辑，如 @Valid 注解配合 Java Bean Validation
            UserRecommendations addedUserRecommendations = this.userRecommendationsService.insert(userRecommendations);
            return ResponseEntity.ok(addedUserRecommendations);
        } catch (Exception e) {
            // 记录日志或处理异常
            return ResponseEntity.status(400).body(null); // 如果输入无效，可以返回400
        }
    }

    /**
     * 编辑数据
     *
     * @param userRecommendations 实体
     * @return 编辑后的实体或错误信息
     */
    @PutMapping
    public ResponseEntity<UserRecommendations> edit(@RequestBody UserRecommendations userRecommendations) {
        try {
            // 验证 userRecommendations 对象和 userId 不为空
            if (userRecommendations == null || userRecommendations.getUserId() == null) {
                return ResponseEntity.status(400).body(null); // 如果输入无效，返回400和null
            }
            UserRecommendations updatedUserRecommendations = this.userRecommendationsService.update(userRecommendations);
            if (updatedUserRecommendations != null) {
                return ResponseEntity.ok(updatedUserRecommendations); // 如果更新成功，返回更新后的实体
            } else {
                return ResponseEntity.status(404).body(null); // 如果没有找到或更新失败，返回404和null
            }
        } catch (IllegalArgumentException e) {
            // 记录日志或处理异常
            return ResponseEntity.status(400).body(null); // 如果输入无效，返回400和null
        } catch (RuntimeException e) {
            // 记录日志或处理异常
            return ResponseEntity.status(500).body(null); // 如果服务内部错误，返回500和null
        }
    }

    /**
     * 删除数据
     *
     * @param userId 主键
     * @return 删除是否成功
     */
    @DeleteMapping("{userId}")
    public ResponseEntity<Boolean> deleteById(@PathVariable("userId") Integer userId) {
        try {
            boolean deleteSuccess = this.userRecommendationsService.deleteById(userId);
            return ResponseEntity.ok(deleteSuccess);
        } catch (Exception e) {
            // 记录日志或处理异常
            return ResponseEntity.status(500).body(false); // 如果服务内部错误，返回500和false
        }
    }
}